package com.ruoyi.web.controller.human;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.human.domain.HuStore;
import com.ruoyi.human.domain.vo.StoreVO;
import com.ruoyi.human.mapper.HuStoreMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class StoreController extends BaseController {
    @Autowired
    private HuStoreMapper storeMapper;
    @Autowired
    private Snowflake snowflake;

    @PostMapping("human/store/add")
    public AjaxResult addStore(@RequestBody StoreVO storeVO) {
        HuStore store = new HuStore();
        //将ProVO中的数据复制到HuStore对象中
        BeanUtils.copyProperties(storeVO, store);
        //调用Mapper方法插入数据
        //分布式雪花算法生成主键

        storeMapper.insertStore(store);
        return success();
    }

    @Cacheable(value = "human", key = "'store::list'", sync = true)
    @PostMapping("human/store/list")
    public TableDataInfo listStores(@RequestBody(required = false) StoreVO vo) {
        startPage(); // 设置分页信息
        List<HuStore> stores = storeMapper.listStores(vo);
        return getDataTable(stores);
    }

}
